package com.bamanker.sms_service.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.bamanker.sms_service.mapper.ToEmailMapper;
import com.bamanker.sms_service.pojo.MyPage;
import com.bamanker.sms_service.pojo.toEmail;
import com.bamanker.sms_service.service.ToEmailService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author bamanker
 */
@Slf4j
@RestController()
@RequestMapping("/manager")
@SaCheckLogin
public class ToEmailController {

    @Autowired
    ToEmailService toEmailService;
    @Autowired
    ToEmailMapper mapper;

    /*restful 部分*/

    @GetMapping("/toEmails")
    @SaCheckPermission(value = "user.get")
    public MyPage<toEmail> list(@RequestParam(value = "start", defaultValue = "1") int start,
                                @RequestParam(value = "size", defaultValue = "5") int size) {
        Page<toEmail> pageInfo = new Page<>(start, size);
        Page<toEmail> page = toEmailService.page(pageInfo);
        MyPage<toEmail> myPage = new MyPage<>();
        myPage.setList(page.getRecords());
        myPage.setPages(page.getPages());
        myPage.setPageNum(page.getCurrent());
        myPage.setSize(page.getSize());
        myPage.setHasPreviousPage(page.hasPrevious());
        myPage.setHasNextPage(page.hasNext());
        myPage.setPrePage(page.getCurrent() - 1);
        myPage.setNextPage(page.getCurrent() + 1);
        long[] navigatepageNums = new long[(int) page.getPages()];
        for (int i = 0, l = 1; i < page.getPages(); i++) {
            navigatepageNums[i] = l++;
        }
        myPage.setNavigatepageNums(navigatepageNums);
        return myPage;
    }

    @GetMapping("/toEmails/{id}")
    @SaCheckPermission(value = "user.get")
    public toEmail get(@PathVariable("id") int id) throws Exception {
        toEmail t = toEmailService.getById(id);
        log.info("{},{}", id, t);
        return t;
    }

    @PostMapping("/toEmails")
    @SaCheckPermission(value = "user.add")
    public String add(@RequestBody toEmail t) {
        mapper.insert(t);
        return "success";
    }

    @DeleteMapping("/toEmails/{id}")
    @SaCheckPermission(value = "user.delete")
    public String delete(toEmail t) {
        toEmailService.removeById(t.getId());
        return "success";
    }

    @PutMapping("/toEmails/{id}")
    @SaCheckPermission(value = "user.update")
    public String update(@RequestBody toEmail t) {
        toEmailService.updateById(t);
        return "success";
    }

    /*页面跳转 部分*/
    @RequestMapping(value = "/listToEmails", method = RequestMethod.GET)
    public ModelAndView listToEmail() {
        ModelAndView mv = new ModelAndView();
        if (StpUtil.isLogin()) {
            mv.addObject("loginId", StpUtil.getLoginId());
            mv.setViewName("listToEmails");
            return mv;
        }
        return new ModelAndView("login");
    }

    @RequestMapping(value = "/editToEmails", method = RequestMethod.GET)
    public ModelAndView editToEmail() {
        return new ModelAndView("editToEmails");
    }

}
